import { _decorator, Asset, AssetManager, AudioClip, Component, Label, Node, Prefab, ProgressBar, resources, SpriteFrame, tween } from 'cc';
import { UIBase } from './UIBase';
import { AudioClipMgr, PrefabMgr, SpecialResMgr, SpriteFrameMgr } from './Card/SpecialResMgr';
import { resmgr } from './Card/ResMgr';
const { ccclass, property } = _decorator;

@ccclass('Loading')
export class Loading extends UIBase {
    private _loadingBar:ProgressBar=null;
    private lbLoadingInfo:Label=null;
    oninit(...rest: any[]): void {
        this._loadingBar=this.getComp("_Pg","ProgressBar");
        this.lbLoadingInfo=this.getComp("_Label","Label");
        resources.loadDir('',null,(finished:number,total:number,item:AssetManager.RequestItem)=>{
            let progress=finished/total;
            this._loadingBar.progress = this._loadingBar.progress < progress?progress:this._loadingBar.progress;
            this.lbLoadingInfo.string = (this._loadingBar.progress*100).toFixed(2)+'%';

        },(err:Error,ret:Asset[])=>{
            if (err) {
                return;
            }
            let Mgrtype = {
                "PrefabMgr": PrefabMgr,
                "SpriteFrameMgr": SpriteFrameMgr,
                "AudioClipMgr": AudioClipMgr
            }
            for (let key in Mgrtype) {
                let objMgr = new Mgrtype[key]();
                resmgr.addMgr(key, objMgr);
            }
            let objType = {
                'PrefabMgr': Prefab,
                'SpriteFrameMgr': SpriteFrame,
                'AudioClipMgr': AudioClip
            }
            for (let res of ret) {
                for (let mgrname in objType) {
                    if (res instanceof objType[mgrname]) {
                        resmgr.getMgr<SpecialResMgr>(mgrname).addRes(res.name, res);
                        break;
                    }
                }

            }
            //console.log(ret);
            //console.log(resmgr);
            this.closeUI("Loading");
            this.openUI("Menu");
        })
    }
}


